module WindSpirit
{
    export class LoadingMC extends egret.DisplayObjectContainer
    {
        public labelWind: egret.TextField;
        public loadingBar: egret.Shape;
        public circle: egret.Shape;
        public barColor = 0xFFA800;
        public constructor()
        {
            super();
            var bgBar: egret.Shape = new egret.Shape;
            bgBar.graphics.beginFill(0xffffff,0.5);//填充
            this.width = WindSpirit.Util.stageW * 0.8;
            this.height = this.width / 8;
            bgBar.graphics.drawRoundRect(0,0,this.width,this.height,this.height);
            bgBar.anchorX = 0.5;
            bgBar.anchorY = 0.5;
            this.addChild(bgBar);
            bgBar = new egret.Shape;
            bgBar.graphics.beginFill(this.barColor);
            this.loadingBar = bgBar;
            var diffH = 10;
            var diffW = 20;
            bgBar.graphics.drawRoundRect(0,0,this.width - diffW,this.height - diffH,this.height - (diffW >> 1));
            bgBar.anchorX = 0.5;
            bgBar.anchorY = 0.5;
            this.addChild(bgBar);

            var circle = new egret.Shape;
            circle.graphics.lineStyle(3,0xffffff);
            circle.graphics.beginFill(0xFDD052);
            circle.graphics.drawCircle(0,0,this.height >> 1);
            circle.x = (this.width - circle.width) >> 1;
            this.addChild(circle);
            this.circle = circle;

            var labelWind = new egret.TextField;
            labelWind.size = this.height*0.57;
            labelWind.text = "风";
            labelWind.anchorY = labelWind.anchorX = 0.5;
            labelWind.x = this.width - circle.width >> 1;
            this.labelWind = labelWind;
            this.addChild(labelWind);
            //this.anchorX = this.anchorY = 0.5;
        }
        public setPregress(persent)
        {
            this.labelWind.rotation = persent * 360 * 5;
            if(persent * 100 % 2 == 0)
            {
                var label = new egret.Shape;
                label.graphics.beginFill(this.barColor);
                label.rotation = 90 * Math.random();
                label.anchorY = 0.5;
                label.anchorX = 0.5;
                var size = 10 * Math.random();
                label.graphics.drawRect(0,0,size,size);
                label.y = Math.random() * (this.height >> 1) - 15;
                label.x = this.width - this.circle.width >> 1;
                egret.Tween.get(label).to({ x: (-this.width >> 1) + 30 },500).call(() =>
                {
                    this.removeChild(label);
                });
                this.addChild(label);
            }
            var mark = new egret.Rectangle(0,0,persent * this.loadingBar.width,this.loadingBar.height);
            this.loadingBar.mask = mark;
            if(persent == 1)
            {
                egret.Tween.get(this.labelWind).to({ scaleX: 0,scaleY: 0 },500).call(() =>
                {
                    this.labelWind.text = "100%";
                    egret.Tween.get(this.labelWind).to({ scaleX: 0.5,scaleY: 0.5 },500).wait(1000).call(() =>
                    {
                        this.dispatchEventWith(egret.Event.COMPLETE);
                    });
                });
            }
        }
    }
}